﻿using System;
using System.IO;
using System.Reflection;
using System.Linq;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.PowerPoint.STAMP.Core;
using NUnit.Framework;
using Shape = Microsoft.Office.Interop.PowerPoint.Shape;


namespace Microsoft.Office.PowerPoint.STAMP.Test
{
    [TestFixture]
    public class UnitTest1
    {
        private Application _pptApp;
        private Slide _slide;
        private Shape _shape;
        private ClosedCaptionsList _closedCaptionsList;

        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            const string fileName = "test ppt.pptx";
            string powerPointTestFile = AssemblyDirectory + @"\files\" + fileName;
            _pptApp = new Application();
            _pptApp.Presentations.Open(powerPointTestFile, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue);
            _slide = _pptApp.Presentations[1].Slides[1];
            _shape = _slide.Shapes[1];
        }

        [SetUp]
        public void Setup()
        {
            _closedCaptionsList = ClosedCaptionsList.Empty;
            _closedCaptionsList.ClosedCaptions.Add(new ClosedCaption
            {
                CaptionText = "One two Three",
                StartTime = new TimeSpan(0, 0, 0, 1),
                EndTime = new TimeSpan(0, 0, 0, 5),
                IsNew = true
            });

            var manager = new CaptionManager(_slide, _shape);

            manager.SetCaptionsListMediaShapeName(_shape.Name, _closedCaptionsList);

            var captionList = Mapper.ToCaptions(_closedCaptionsList);

            manager.RenderClosedCaptions(captionList);

            manager.SaveCaptions();
        }

        [TearDown]
        public void TearDown()
        {
            var manager = new CaptionManager(_slide, _shape);
            manager.RemoveSlideCaptions();
        }

        [TestFixtureTearDown]
        public void TestFixtureTearDown()
        {
            _pptApp.Quit();
        }

        [Test]
        public void ShowShapes()
        {
            var manager = new CaptionManager(_slide, _shape);

            manager.ShowCaptions();

            var list = ShapeFinder.FindShapesByMediaName(_slide, _shape.Name);

            Assert.AreEqual(list.Count, list.Where(a => a.Visible == MsoTriState.msoTrue).Count());
        }

        [Test]
        public void HideShapes()
        {
            var manager = new CaptionManager(_slide, _shape);

            manager.HideCaptions();

            var list = ShapeFinder.FindShapesByMediaName(_slide, _shape.Name);

            Assert.AreEqual(list.Count, list.Where(a => a.Visible == MsoTriState.msoFalse).Count());
        }

        [Test]
        public void CreateCaptions()
        {
            var manager = new CaptionManager(_slide, _shape);

            Assert.AreEqual(_closedCaptionsList.ClosedCaptions.Count, manager.CaptionShapes.Count);
        }

        [Test]
        public void DeleteCaptions()
        {
            var manager = new CaptionManager(_slide, _shape);

            Assert.AreEqual(_closedCaptionsList.ClosedCaptions.Count, manager.CaptionShapes.Count);

            manager.RemoveSlideCaptions();

            Assert.AreEqual(0, manager.CaptionShapes.Count);
        }

        static public string AssemblyDirectory
        {
            get
            {
                string codeBase = Assembly.GetExecutingAssembly().CodeBase;
                var uri = new UriBuilder(codeBase);
                string path = Uri.UnescapeDataString(uri.Path);
                return Path.GetDirectoryName(path);
            }
        }
    }
}
